package com.taijihuabao.modules.controller;

import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.common.redisconfig.UserLoad;
import com.taijihuabao.modules.entity.RaiseSalaryEntity;
import com.taijihuabao.modules.entity.UserEntity;
import com.taijihuabao.modules.service.RaiseSalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 提薪审批管理
 * @Author : xjt
 */
@Api(tags = "提薪审批管理")
@RestController
@RequestMapping("/api/v1/raiseSalary")
public class RaiseSalaryController {

    @Autowired
    private RaiseSalaryService raiseSalaryService;

    /**
     * 请假流程定义
     *
     * @return
     */
    @GetMapping("/deploy")
    @ApiOperation("流程定义")
    public JsonResult deploy() {
        raiseSalaryService.deploy();
        return JsonResult.success();
    }

    /**
     * 新增请假审批并启动流程
     *
     * @return approvalEntity
     */
    @PostMapping("/start")
    @ApiOperation("新增离职审批并开始流程")
    public JsonResult insertApproval(@RequestBody RaiseSalaryEntity raiseSalaryEntity){
        raiseSalaryService.startProcess(raiseSalaryEntity);
        return JsonResult.success(raiseSalaryEntity);
    }

    /**
     * 根据登录id查询待办审批
     * @return
     */
    @GetMapping("/selector")
    @ApiOperation("根据登录id查询待办审批")
    public JsonResult findApprovalAll(){
        //获取登录用户
        UserEntity loginUser = UserLoad.getUser();
        //根据登录id查询待办审批
        List<RaiseSalaryEntity> raiseSalaryEntities = raiseSalaryService.findTaskByUserId(String.valueOf(loginUser.getId()));
        return JsonResult.success(raiseSalaryEntities);
    }

    /**
     * 审核待办审批单
     * taskId 审核任务
     * agree  审核结果
     * @return approvalEntity
     */
    @PostMapping("/approval")
    @ApiOperation("根据登录id审批待办审批")
    public JsonResult approve( @RequestBody RaiseSalaryEntity raiseSalaryEntity){
        //获取登录用户
        UserEntity loginUser = UserLoad.getUser();
        raiseSalaryService.approvalTask(String.valueOf(loginUser.getId()), raiseSalaryEntity);
        return JsonResult.success();
    }
}
